package com.hildeberto.home.client;

import com.conseilimmo.bo.util.ConstantesConseilImmo;
import com.conseilimmo.repository.BatimentBsn;
import com.hildeberto.home.util.EntityManagerHelper;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javax.persistence.EntityManager;
import objetsBd.BatimentBd;

/**
 *
 * @author verpoorten
 */
public class ListeBatimentController implements Initializable {

    private static final Logger LOGGER = Logger.getLogger(ListeBatimentController.class.getName());
    @FXML
    TableView<BatimentBd> tableBatiments;
    @FXML
    TableColumn<BatimentBd, String> denominationCol;
    @FXML
    TableColumn<BatimentBd, String> rueCol;
    @FXML
    TableColumn<BatimentBd, String> localiteCol;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        /*denominationCol.setCellValueFactory(new PropertyValueFactory<BatimentBd, String>("denomination"));
         rueCol.setCellValueFactory(new PropertyValueFactory<BatimentBd, String>("ruee"));
         localiteCol.setCellValueFactory(new PropertyValueFactory<BatimentBd, String>("Localité"));*/

        tableBatiments.getItems().setAll(listeBatiments());
        tableBatiments.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            // this method will be called whenever user selected row
            @Override
            public void changed(ObservableValue observale, Object oldValue, Object newValue) {
                BatimentBd selectedUser = (BatimentBd) newValue;
                LOGGER.log(Level.INFO, "id sel:{0}", selectedUser.getIdBatiment());
            }
        });
        denominationCol.setSortType(TableColumn.SortType.ASCENDING);
        rueCol.setSortType(TableColumn.SortType.ASCENDING);

    }

    private BatimentBd[] listeBatiments() {
        if(!MainApp.getModeAvecBd()){
        List<BatimentBd> batiments = new ArrayList<>();
         BatimentBd batiment = new BatimentBd();
         batiment.setIdBatiment(1L);
         batiment.setDenomination("denom1");
         batiment.setAdresse("rue du centre 1");
         batiment.setAdresseLocalite("lisogne");
         BatimentBd batiment2 = new BatimentBd();
         batiment2.setIdBatiment(2L);
         batiment2.setDenomination("denom2");
         batiment2.setAdresse("rue du centre 2");
         batiment.setAdresseLocalite("lisogne");
         batiments.add(batiment);
         batiments.add(batiment2);
         return batiments.toArray(new BatimentBd[batiments.size()]);
                 }else{
        EntityManager em = EntityManagerHelper.EM.open();
        BatimentBsn batimentBsn = new BatimentBsn();
        List<BatimentBd> batiments = batimentBsn.trouverListeBatiments();
        EntityManagerHelper.EM.close();
        return batiments.toArray(new BatimentBd[batiments.size()]);
        }

    }

    @FXML
    private void btnVoirDetailBatiment(ActionEvent event) throws Exception {
        BatimentController controller = MainApp.getBatimentController();
        LOGGER.log(Level.INFO, "oucou");
        if (tableBatiments.getSelectionModel().getSelectedItem() != null) {
            LOGGER.log(Level.INFO, "coucou on veut voir le batiment" + tableBatiments.getSelectionModel().getSelectedItem().getIdBatiment());
            controller.changerDeBatiment(tableBatiments.getSelectionModel().getSelectedItem());
            
            FXMLController controllerEcran = MainApp.getController();
            controllerEcran.afficherEcran(ConstantesConseilImmo.URL_BATIMENT);
        }
    }

    @FXML
    private void dupliquerBatimentAction(ActionEvent event) {

    }

    @FXML
    private void ajouterBatimentAction(ActionEvent event) {

    }

    @FXML
    private void supprimerBatimentAction(ActionEvent event) {

    }

    @FXML
    private void voirBatimentAction(ActionEvent event) {

    }
}

